package cn.bgs.controller;

import cn.bgs.common.R;
import cn.bgs.pojo.PaymentType;
import cn.bgs.pojo.User;
import cn.bgs.service.IPaymentTypeService;
import cn.bgs.utils.RedisUtil;
import cn.bgs.vo.Livingexpenses;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import cn.bgs.common.R;
import cn.bgs.service.IPaymentTypeService;
import cn.bgs.vo.Bill;
import cn.bgs.vo.LikeBill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 缴费类型表(账单表) 前端控制器
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/payment-type")
public class PaymentTypeController {

    @Autowired
    private IPaymentTypeService iPaymentTypeService;
    @Autowired
    private   RedisUtil redisUtil;

    /**
     * 获取用户未缴纳账单
     * @param id
     * @return
     */
    @GetMapping("/cfMypayment")
    public R cfMypayment(Integer id){
        List<Livingexpenses> mypaymentkeylist= (List<Livingexpenses>) redisUtil.get("Mypaymentkey");
        User user =iPaymentTypeService.getUsermoney(id);
        Double blance=user.getBalance();
        if (mypaymentkeylist==null){
            List<Livingexpenses> list=iPaymentTypeService.cfMypayment(id);
            redisUtil.set("Mypaymentkey",list);
            return  R.ok().data("list",list).data("balance",blance);
        }
        return R.ok().data("list",mypaymentkeylist).data("balance",blance);
    }

    /**
     * 添加当前用户缴费账单
     * @param paymentType
     * @return
     */
    @PostMapping("/cfInsertPayment")
    public R cfInsertPayment(@RequestBody PaymentType paymentType){
         Integer userId=paymentType.getUserId();
         int i=iPaymentTypeService.cfInsertPayment(paymentType);
         if (i>0){
             redisUtil.del("Mypaymentkey");
             List<Livingexpenses> list= iPaymentTypeService.cfMypayment(userId);
             redisUtil.set("Mypaymentkey",list);
             return R.ok().code(2000);
         }
         else {
             return R.ok().code(3000);
         }
    }

    /**
     * 支付
     * @param id
     * @param money
     * @param payId
     * @return
     */
    @GetMapping("/cfPayment")
    public R cfPayment(@RequestParam("id") Integer id,@RequestParam("money") Double money,@RequestParam("payId") Integer payId){
        User user =iPaymentTypeService.getUsermoney(id);
        Double balance=user.getBalance();
        if (balance>money){
            Double paybalance=balance-money;
            int i=iPaymentTypeService.cfPayment(paybalance,id);
            if (i>0){
                iPaymentTypeService.uupdatePaymentType(payId);
                redisUtil.del("Mypaymentkey");
                List<Livingexpenses> list=iPaymentTypeService.cfMypayment(id);
                redisUtil.set("Mypaymentkey",list);
                return R.ok().code(2000);
            }else {
                return R.ok().data("error","支付失败");
            }
        }else {
            return R.ok().data("error","余额不足,请充值！");
        }
    }

    /**
     * 充值
     * @param id
     * @param money
     * @return
     */
    @GetMapping("/cfRecharge")
    public R cfRecharge(@RequestParam("id") Integer id ,@RequestParam("money") Double money){
        User user =iPaymentTypeService.getUsermoney(id);
        Double balance=user.getBalance();
        Double newmoney=balance+money;
        int i=iPaymentTypeService.cfRecharge(newmoney,id);
        if (i>0){
            User user1 =iPaymentTypeService.getUsermoney(id);
            Double newblance=user1.getBalance();
            return R.ok().code(2000).data("newblance",newblance);
        }
       return R.ok().data("error","充值失败");
    }

    /**
     * 查询所有支付账单
     * @param id
     * @return
     */
    @GetMapping("/cfGetAllcost")
    public R cfGetAllcost(@RequestParam("id") Integer id,@RequestParam("type") String type,@RequestParam("month") String month){
//        List<PaymentType> cfGetAllcostkeylist= (List<PaymentType>) redisUtil.get("cfGetAllcostkey");
//        if (cfGetAllcostkeylist==null){
            List<PaymentType> list=iPaymentTypeService.cfGetAllcost(id,type,month);
//            redisUtil.set("cfGetAllcostkey",list);
            return R.ok().data("list",list);
        }
//        return R.ok().data("list",cfGetAllcostkeylist);
//    }




    @GetMapping("/findBill")
    public R findBill(){
        return iPaymentTypeService.findBill("", "", "", "");
    }

    @PostMapping("/findIdBill")
    public R findIdBill(@RequestBody LikeBill likeBill){
        return iPaymentTypeService.findBill(likeBill.getPaymentState(), likeBill.getType(), likeBill.getNameType(), likeBill.getName());
    }

    @GetMapping("/delBill/{paymentTypeId}")
    public R delBill(@PathVariable("paymentTypeId")Integer paymentTypeId){

        return iPaymentTypeService.delBiil(paymentTypeId);
    }

    @PostMapping("/addBill")
    public R addBill(@RequestBody Bill bill){

        System.out.println(bill);

        return iPaymentTypeService.addBill(bill);
    }

    @PostMapping("/updateBill")
    public R updateBill(@RequestBody Bill bill){
        return iPaymentTypeService.updateBill(bill);
    }

}
